﻿Imports System.ComponentModel
Imports System.IO
Imports System.Xml.Serialization

Public Class UserInfo
    Implements INotifyPropertyChanged

    Private _userName As String, _message As String, _photoLocation As String, _saveFolder As String
    Private _filename As String
    Private _lateMessage As String = "I'm late!", _noReturnTimeMessage As String = "Unknown"
    Private _returnTime As DateTime
    Private _photosEnabled As Boolean

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Sub New()
        ' Reasonable defaults
        _photoLocation = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
        _userName = Environment.UserName
        _returnTime = DateTime.MinValue

        _saveFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WhileAway")
        Filename = System.IO.Path.Combine(_saveFolder, "UserInfo.dat")
    End Sub

    Public Property Filename() As String
        Get
            Return _filename
        End Get
        Set(ByVal value As String)
            _filename = value
        End Set
    End Property

    Public Property UserName() As String
        Get
            Return _userName
        End Get
        Set(ByVal value As String)
            _userName = value
            PropChange("UserName")
        End Set
    End Property

    Public Property LateMessage() As String
        Get
            Return _lateMessage
        End Get
        Set(ByVal value As String)
            _lateMessage = value
            PropChange("LateMessage")
        End Set
    End Property

    Public Property NoReturnTimeMessage() As String
        Get
            Return _noReturnTimeMessage
        End Get
        Set(ByVal value As String)
            _noReturnTimeMessage = value
            PropChange("NoReturnTimeMessage")
        End Set
    End Property

    Public Property PhotoLocation() As String
        Get
            Return _photoLocation
        End Get
        Set(ByVal value As String)
            _photoLocation = value
            PropChange("PhotoLocation")
        End Set
    End Property

    Public Property ReturnTime() As DateTime
        Get
            Return _returnTime
        End Get
        Set(ByVal value As DateTime)
            _returnTime = value
            PropChange("ReturnTime")
        End Set
    End Property

    Public Property Message() As String
        Get
            Return _message
        End Get
        Set(ByVal value As String)
            _message = value
            PropChange("Message")
        End Set
    End Property

    Public Property PhotosEnabled() As Boolean
        Get
            Return _photosEnabled
        End Get
        Set(ByVal value As Boolean)
            _photosEnabled = value
            PropChange("PhotosEnabled")
        End Set
    End Property

    Public Property ReturnTimeRemaining() As TimeSpan
        Get
            If _returnTime = DateTime.MinValue Then
                Return TimeSpan.MinValue
            Else
                Return _returnTime - DateTime.Now
            End If
        End Get
        Set(ByVal value As TimeSpan)
            PropChange("ReturnTimeRemaining")
        End Set
    End Property

    Public Sub Load()
        If Not File.Exists(Filename) Then
            Return
        End If

        Try
            Dim ser As New XmlSerializer(GetType(UserInfo))
            Using str As New StreamReader(Filename)
                Dim toLoad As UserInfo = DirectCast(ser.Deserialize(str), UserInfo)

                Me.Message = toLoad.Message
                Me.PhotoLocation = toLoad.PhotoLocation
                Me.PhotosEnabled = toLoad.PhotosEnabled
                Me.ReturnTime = toLoad.ReturnTime
                Me.UserName = toLoad.UserName
            End Using
        Catch
        End Try
    End Sub

    Public Sub Save()
        Directory.CreateDirectory(_saveFolder)

        Try
            Dim ser As New XmlSerializer(GetType(UserInfo))
            Using str As New StreamWriter(Filename)
                ser.Serialize(str, Me)
            End Using
        Catch
        End Try
    End Sub

    Public Sub UpdateReturnTimeRemaining()
        PropChange("ReturnTimeRemaining")
    End Sub

    Private Sub PropChange(ByVal name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub

End Class
